//
//  RegistrationViewController.m
//  FirstMatchThree
//
//  Created by Petar on 5/18/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "RegistrationViewController.h"
#import "CommunicationManager.h"
#import "Globals.h"
#import "AudioController.h"
#import "Tracker.h"

#define MAX_LENGTH 15

@interface RegistrationViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;

@end

@implementation RegistrationViewController


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    _usernameTextField.delegate = self;
    [_usernameTextField becomeFirstResponder];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:DISMISS_REGISTRATION_VC object:nil];
    
}


-(void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


- (IBAction)didPressRegisterButton:(id)sender
{
    
    [[AudioController sharedInstance] playButtonClick];
    
    // Check for symbol ; in the name for sequrity
    BOOL isValidUsername = [self checkForValidUsername:_usernameTextField.text];
    
    if ( isValidUsername == YES ) {
        
        NSString *username = _usernameTextField.text;
        
        [[NSUserDefaults standardUserDefaults] setObject:username forKey:USERNAME_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [[CommunicationManager sharedManager] connectToWebService];
        
    } else {
        
        // show alert for not valid Username
        NSString *message = [NSString stringWithFormat:@"'%@' is not a valid username.", _usernameTextField.text];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    
}


- (BOOL)checkForValidUsername:(NSString *)username
{
    
    if ( [username length] == 0 ) {
        return NO;
    }
    
    // Check for symbol ';'
    for (int i = 0; i < [username length]; i++) {
        
        if ( [username characterAtIndex:i] == ';' ) {
            return NO;
        }
        
    }
    
    return YES;
    
}


- (void)dismiss
{
    
    NSDate *date = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setObject:date forKey:REGISTRATION_DATE_KEY];
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:USER_MONEY_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [Tracker trackEvent:@"Register User" withParams:nil];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSUInteger oldLength = [textField.text length];
    NSUInteger replacementLength = [string length];
    NSUInteger rangeLength = range.length;
    
    NSUInteger newLength = oldLength - rangeLength + replacementLength;
    
    BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;
    
    return newLength <= MAX_LENGTH || returnKey;
}


-(BOOL)prefersStatusBarHidden
{
    return YES;
}


@end
